const _ = require('lodash')
const fp = require('lodash/fp')

const trace = _.curry(function (tag, v) {
  console.log(tag, v)
  return v
})

// 数据
// horsepower 马力，dollar_value 价格，in_stock 库存

const cars = [
  {
    name: 'Ferrari FF',
    horsepower: 660,
    dollar_value: 700000,
    in_stock: true
  },
  { name: 'Spyker C12', horsepower: 650, dollar_value: 648000, in_stock: false },
  { name: 'JAGUAR xkr-s', horsepower: 550, dollar_value: 132000, in_stock: false },
  { name: 'Audi R8', horsepower: 525, dollar_value: 114200, in_stock: false },
  { name: 'Aston Martin Onr-77', horsepower: 750, dollar_value: 185000, in_stock: true },
  { name: 'Pagani Huayra', horsepower: 700, dollar_value: 130000, in_stock: false },
]

console.log('========================练习1==================================')

// 练习1 使用函数组合fp.flowRight()重新实现
let isLastInStock = function (cars) {
  // 获取最后一条数据
  let last_car = fp.last(cars)
  // 获取最后一条数据的 in_stock 属性值
  return fp.prop('in_stock', last_car)
}

const isLastInStockNew = fp.flowRight(fp.prop('in_stock'), trace('last'), cars => fp.last(cars))

let res = isLastInStock(cars)
let resNew = isLastInStockNew(cars)
console.log(res)
console.log(resNew)


console.log('========================练习2==================================')


// 练习2
// 使用fp.flowRight()、fp.prop()、fp.first()获取第一个car的 name

const isFirstName = fp.flowRight(fp.prop("name"), trace("first"), cars => fp.first(cars))

let res2 = isFirstName(cars)
console.log(res2)

console.log('========================练习3==================================')

// 练习3
// 使用帮助函数 _average 重构 averageDollarValue，使用函数组合的方式实现
let _average = function (xs) {
  return fp.reduce(fp.add, 0, xs) / xs.length
}

//<-无需改动

let averageDollarValue = function (cars) {
  let dollar_values = fp.map(function (car) {
    return car.dollar_value
  }, cars)
  return _average(dollar_values)
}

const compose = function (f , g){
  return function (x){
    return f(g(x))
  }
}

let dollar_values = cars => fp.map(car => car.dollar_value, cars)

const averageDollarValueNew = compose(_average, dollar_values)

const res3 = averageDollarValue(cars)
const res3New = averageDollarValueNew(cars)
console.log(res3)
console.log(res3New)


console.log('========================练习4==================================')

// 练习4
// 使用flowRight 写一个 sanitizeNames() 函数，返回一个下划线连接的小写字符串，把数组中的 name 转换成为这种形式：例如: sanitizeNames(['Hello World'])=>['hello_world']

let _underscore = fp.replace(/\W+/g, '_') //<--无需改动，并在sanitizeNames中使用它

const sanitizeNames = fp.map(fp.flowRight(_underscore, fp.toLower, fp.split(',')))

console.log(sanitizeNames(['Hello World']))
console.log(sanitizeNames(['Hello World', 'Dong Bing Chen']))


console.log('***********************代码题2**********************************')
const { Maybe, Container } = require('./support')

console.log('========================练习1==================================')
// 练习1
// 使用fp.add(x,y) 和 fp.map(f,x) 创建一个能让functor 里的值增加的函数ex1

let maybe = Maybe.of([5, 6, 11])
let ex1 = x => fp.map(fp.add(1), x)

console.log(maybe.map(ex1))

console.log('========================练习2==================================')
// 练习2
// 实现一个函数ex2，能够使用fp.first 获取列表的第一个元素

let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = x => fp.first(x)

console.log(xs.map(ex2))

console.log('========================练习3==================================')
// 练习3
// 实现一个函数ex3，使用safeProp 和 fp.first 找到user 的名字的首字母

let safeProp = fp.curry(function (x, o) {
  return Maybe.of(o[x])
})
let user = { id: 2, name: 'Albert' }
let user1 = { id: 2, age: 18, name: 'Albert' }
const index = (obj, keyName) => Object.keys(obj).indexOf(keyName)
let ex3 = (obj, keyName) => safeProp(index(obj, keyName), fp.map(fp.first, obj))

console.log(ex3(user,'name'))
console.log(ex3(user1,'name'))

console.log('========================练习4==================================')
// 练习4
// 使用Maybe 重写 ex4，不要有if语句

let ex4 = function(n){
  if(n){
    return parseInt(n)
  }
}

let ex5 = function(n){
  return Maybe.of(n).map(n => parseInt(n))
}


console.log(ex4('12'))
console.log(ex5('22'))
